<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<?php
        if( !isset($_COOKIE["username"]) ){
                echo "<script>window.location.href='login.php'</script>";
        }else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Search</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />

<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>

<script src="js/search.js" type="text/javascript"></script>


<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>



<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
	droplinemenu.buildmenu("nav")

	$(document).ready(function(){
		var datepicker_CurrentInput;  
        $.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        $(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		$("#birth").datepicker({
			dateFormat:'dd-mm-yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});
</script>
</head>
<body>
        <div id="container">
                <div id="header"></div>
                <?php include_once 'nav.php';?>
               
                <?php
                        $ini_array = parse_ini_file("config/config.ini");
                       
                        $conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
                        mysqli_set_charset($conn, "utf8");
                        ini_set('default_charset', "UTF-8");
                        $patientNo = "";
                        $firstName = "";
                        $surname = "";
                        
                        $isFlag = false;
                        
                        if( isset($_POST["firstName"]) ){
                                $firstName = mysqli_real_escape_string($conn,$_POST["firstName"]);
                                $isFlag = true;
                        }
                        if( isset($_POST["surname"]) ){        
                                $surname = mysqli_real_escape_string($conn,$_POST["surname"]);
                                $surname = mysqli_real_escape_string($conn, $surname);
                                $isFlag = true;
                        }
                       
                        if( isset($_POST["patientNo"])){
                                $patientNo = mysqli_real_escape_string($conn,$_POST["patientNo"]);
                                $patientNo = mysqli_real_escape_string($conn, $patientNo);
                                $isFlag = true;
                        }
                        
                        if( isset($_POST["birth"])){
                        	$birth = mysqli_real_escape_string($conn,$_POST["birth"]);
                        	$birth = mysqli_real_escape_string($conn, $birth);
                        	$isFlag = true;
                        }
                        
                        
                        $selectRow = 0;
                        
                        if( $isFlag == true ){
                       
	                        $flag = false;
	                        $sql = "select * from patient where ";
	                        if( strcmp($patientNo,"") != 0 ){
	                                $sql .= " patient_no=".$patientNo;
	                                $flag = true;
	                        }
	                       
	                        if( strcmp($firstName, "") != 0 ){
	                                if( $flag == true ){
	                                        $sql .= " and ";
	                                }
	                                $flag = true;
	                                $sql .= "  first_name like '%".$firstName."%'";
	                        }
	                       
	                        if( strcmp($surname, "") != 0 ){
	                                if( $flag == true ){
	                                        $sql .= " and ";
	                                }
	                                $sql .= "  surname like '%".$surname."%'";
	                        }
	                        
	                        if( strcmp($birth, "") != 0 ){
									if( $flag == true ){
										$sql .= " and  ";
									}
									$sql .= " Date_birth=str_to_date('".$birth."', '%d-%m-%Y')";
							}
	                       
	                        $result = mysqli_query($conn, $sql);
	                        $selectRow = mysqli_num_rows($result);
                        }
                ?>
                <?php include_once 'search_patient.php';?>

                <div id="main_content">
                        <div id="result" <?php if( $selectRow == 0 ){ echo "style=\"display:none\"";}?>>
                                <div style="position:relative;left:100px;"><label>Result:</label></div>
                                <div style="position:relative;top:-1em;margin:0px auto;width:620px;">
                                        <div style="display:inline-block;width:200px;"><label>Patient No.</label></div>
                                        <div style="display:inline-block;width:200px;"><label>Patient First Name</label></div>
                                        <div style="display:inline-block;width:200px;"><label>Patient Surname</label></div>
                                </div>
                                <div id="nameSearchList" style="margin:10px auto;width:600px;height:600px;overflow:auto;">
                                        <table style="width:580px;">
                                                <?php
                                                        if( $selectRow != 0 ){
                                                                while( $row = mysqli_fetch_array($result) ){
                                                                        $patientNo = $row["Patient_no"];
                                                                        //echo $patientNo;
                                                                        $firstNameInDB = $row["First_name"];
                                                                        $surnameInDB = $row["Surname"];
                                                                        //echo $surnameInDB;
                                                ?>
                                                <tr>
                                                        <td style="width:33%;"><a href="patient_account.php?patientNo=<?php echo $patientNo?>"><?php echo $patientNo?></a></td>
                                                        <td style="width:33%;"><label><?php echo $firstNameInDB?></label></td>
                                                        <td style="width:33%;"><label><?php echo $surnameInDB?></label></td>
                                                </tr>
                                                <?php  
                                                                }
                                                        }
                                                ?>
                                        </table>
                                </div>
                        </div>
                        <div id="noResult" style="text-align:center;<?php if( $selectRow != 0 ){ echo "display:none";}?>">
                                <label>Search No Result</label>
                                <input type="button" name="NewPatient" onClick="window.location.href='patient_info.php'" value="New Patient" />
                        </div>
                </div>
                <?php include_once 'footer.php';?>
        </div>
</body>
</html>
